﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;
namespace BUS
{
    public class AutoComplete
    {
        static public int MAX_SUGGESTION = 7;

        private DichVuDAO _dichVuDAO = null;
        private DuLieuDAO _duLieuDAO = null;
        private DuongDAO _duongDAO = null;
        private PhuongDAO _phuongDAO = null;
        private QuanHuyenDAO _quanHuyenDAO = null;
        private TenDiaDiemDAO _tenDiaDiemDAO = null;
        private TinhThanhDAO _tinhThanhDAO = null;
        private TuKhoaDiaDiemDAO _tuKhoaDiaDiemDAO = null;
        private TuKhoaDichVuDAO _tuKhoaDichVuDAO = null;
        private TuKhoaDuongDAO _tuKhoaDuongDAO = null;
        private TuKhoaPhuongDAO _tuKhoaPhuongDAO = null;
        private TuKhoaQuanHuyenDAO _tuKhoaQuanHuyenDAO = null;
        private TuKhoaTinhThanhDAO _tuKhoaTinhThanhDAO = null;

        private List<string> _lstResult = null;

        public class SuggestObject
        {
            private string _original;

            public string Original
            {
                get { return _original; }
                set { _original = value; }
            }

            private string _keyword;

            public string Keyword
            {
                get { return _keyword; }
                set { _keyword = value; }
            }
        }

        // danh cho suggest - danh sach DUONG, PHUONG, QUANHUYEN tren bo nho
        private SortedList<string, string> _slstDichVuDTO = null;
        private SortedList<string, string> _slstDuongDTO = null;
        private SortedList<string, string> _slstPhuongDTO = null;
        private SortedList<string, string> _slstQuanHuyenDTO = null;

        public AutoComplete()
        {
            try
            {
                this._dichVuDAO = new DichVuDAO();
                this._duLieuDAO = new DuLieuDAO();
                this._duongDAO = new DuongDAO();
                this._phuongDAO = new PhuongDAO();
                this._quanHuyenDAO = new QuanHuyenDAO();
                this._tenDiaDiemDAO = new TenDiaDiemDAO();
                this._tinhThanhDAO = new TinhThanhDAO();
                this._tuKhoaDiaDiemDAO = new TuKhoaDiaDiemDAO();
                this._tuKhoaDichVuDAO = new TuKhoaDichVuDAO();
                this._tuKhoaDuongDAO = new TuKhoaDuongDAO();
                this._tuKhoaPhuongDAO = new TuKhoaPhuongDAO();
                this._tuKhoaQuanHuyenDAO = new TuKhoaQuanHuyenDAO();
                this._tuKhoaTinhThanhDAO = new TuKhoaTinhThanhDAO();

                this._lstResult = new List<string>();

                // nap danh sach DUONG, PHUONG, QUANHUYEN vao bo nho
                List<QuanHuyenDTO> lstQuanHuyenDTO = this.GetAllQuanHuyenDTO();
                List<PhuongDTO> lstPhuongDTO = this.GetAllPhuongDTO();
                List<DuongDTO> lstDuongDTO = this.GetAllDuongDTO();
                List<DichVuDTO> lstDichVuDTO = this.GetAllDichVuDTO();

                this._slstQuanHuyenDTO = new SortedList<string, string>();
                foreach (QuanHuyenDTO quanHuyenDTO in lstQuanHuyenDTO)
                {
                    try
                    {
                        this._slstQuanHuyenDTO.Add(Utilities.FormatInputData(quanHuyenDTO.TenQuanHuyen), quanHuyenDTO.TenQuanHuyen);
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                this._slstPhuongDTO = new SortedList<string, string>();
                foreach (PhuongDTO phuongDTO in lstPhuongDTO)
                {
                    try
                    {
                        this._slstPhuongDTO.Add(Utilities.FormatInputData(phuongDTO.TenPhuong), phuongDTO.TenPhuong);
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                this._slstDuongDTO = new SortedList<string, string>();
                foreach (DuongDTO duongDTO in lstDuongDTO)
                {
                    try
                    {
                        this._slstDuongDTO.Add(Utilities.FormatInputData(duongDTO.TenDuong), duongDTO.TenDuong);
                    }
                    catch (System.Exception ex)
                    {

                    }
                }

                this._slstDichVuDTO = new SortedList<string, string>();
                foreach (DichVuDTO dichVuDTO in lstDichVuDTO)
                {
                    try
                    {
                        this._slstDichVuDTO.Add(Utilities.FormatInputData(dichVuDTO.TenDichVu), dichVuDTO.TenDichVu);
                    }
                    catch (System.Exception ex)
                    {

                    }
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public List<QuanHuyenDTO> GetAllQuanHuyenDTO()
        {
            List<QuanHuyenDTO> lstQuanHuyenDTO = null;
            try
            {
                DataProvider.Connect();
                lstQuanHuyenDTO = this._quanHuyenDAO.GetAllQuanHuyenDTO();
            }
            catch (System.Exception ex)
            {
                lstQuanHuyenDTO = new List<QuanHuyenDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstQuanHuyenDTO;
        }

        public List<PhuongDTO> GetAllPhuongDTO()
        {
            List<PhuongDTO> lstPhuongDTO = null;
            try
            {
                DataProvider.Connect();
                lstPhuongDTO = this._phuongDAO.GetAllPhuongDTO();
            }
            catch (System.Exception ex)
            {
                lstPhuongDTO = new List<PhuongDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstPhuongDTO;
        }

        public List<DuongDTO> GetAllDuongDTO()
        {
            List<DuongDTO> lstDuongDTO = null;
            try
            {
                DataProvider.Connect();
                lstDuongDTO = this._duongDAO.GetAllDuongDTO();
            }
            catch (System.Exception ex)
            {
                lstDuongDTO = new List<DuongDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstDuongDTO;
        }

        public List<DichVuDTO> GetAllDichVuDTO()
        {
            List<DichVuDTO> lstDichVuDTO = null;
            try
            {
                DataProvider.Connect();
                lstDichVuDTO = this._dichVuDAO.GetAllDichVuDTO();
            }
            catch (System.Exception ex)
            {
                lstDichVuDTO = new List<DichVuDTO>();
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstDichVuDTO;
        }

        private void Analysis(string strInput, string tenDichVu, string tenDuong, string tenPhuong, string tenQuanHuyen)
        {
            if (MAX_SUGGESTION == this._lstResult.Count)
            {
                return;
            }

            if (0 == strInput.Length)
            {
                string strTemp = String.Empty;
                if (tenDichVu.Length > 0)
                {
                    strTemp += tenDichVu;
                    strTemp += " ";
                }

                if (tenDuong.Length > 0)
                {
                    strTemp += tenDuong;
                    strTemp += " ";
                }

                if (tenPhuong.Length > 0)
                {
                    strTemp += tenPhuong;
                    strTemp += " ";
                }

                if (tenQuanHuyen.Length > 0)
                {
                    strTemp += tenQuanHuyen;
                    strTemp += " ";
                }

                strTemp = strTemp.Trim();
                this._lstResult.Add(strTemp);
            }

            if (0 == tenDichVu.Length)
            {
                List<string> lstDichVu = this._slstDichVuDTO.Where(x => x.Key.StartsWith(strInput) | strInput.StartsWith(x.Key)).Select(x => x.Value).ToList<string>();
                string strTemp = String.Empty;
                for (int i = 0; i < lstDichVu.Count; i++)
                {
                    string keyword = Utilities.FormatInputData(lstDichVu[i]);
                    if (keyword.StartsWith(strInput))
                    {
                        strTemp = String.Empty;
                    }
                    else
                    {
                        strTemp = strInput.Replace(keyword, String.Empty).Trim();
                    }

                    Analysis(strTemp, lstDichVu[i], tenDuong, tenPhuong, tenQuanHuyen);
                    if (lstDichVu[i].ToLower() != Utilities.FormatInputData(lstDichVu[i]))
                    {
                        Analysis(strTemp, Utilities.FormatInputData(lstDichVu[i]), tenDuong, tenPhuong, tenQuanHuyen);
                    }
                }
            }

            if (0 == tenDuong.Length)
            {
                List<string> lstDuong = this._slstDuongDTO.Where(x => x.Key.StartsWith(strInput) | strInput.StartsWith(x.Key)).Select(x => x.Value).ToList<string>();
                string strTemp = String.Empty;
                for (int i = 0; i < lstDuong.Count; i++)
                {
                    string keyword = Utilities.FormatInputData(lstDuong[i]);
                    if (keyword.StartsWith(strInput))
                    {
                        strTemp = String.Empty;
                    }
                    else
                    {
                        strTemp = strInput.Replace(keyword, String.Empty).Trim();
                    }

                    Analysis(strTemp, tenDichVu, lstDuong[i], tenPhuong, tenQuanHuyen);
                    if (lstDuong[i].ToLower() != Utilities.FormatInputData(lstDuong[i]))
                    {
                        Analysis(strTemp, tenDichVu, Utilities.FormatInputData(lstDuong[i]), tenPhuong, tenQuanHuyen);
                    }
                }
            }

            if (0 == tenPhuong.Length)
            {
                List<string> lstPhuong = this._slstPhuongDTO.Where(x => x.Key.StartsWith(strInput) | strInput.StartsWith(x.Key)).Select(x => x.Value).ToList<string>();
                string strTemp = String.Empty;
                for (int i = 0; i < lstPhuong.Count; i++)
                {
                    string keyword = Utilities.FormatInputData(lstPhuong[i]);
                    if (keyword.StartsWith(strInput))
                    {
                        strTemp = String.Empty;
                    }
                    else
                    {
                        strTemp = strInput.Replace(keyword, String.Empty).Trim();
                    }

                    Analysis(strTemp, tenDichVu, tenDuong, lstPhuong[i], tenQuanHuyen);
                    if (lstPhuong[i].ToLower() != Utilities.FormatInputData(lstPhuong[i]))
                    {
                        Analysis(strTemp, tenDichVu, tenDuong, Utilities.FormatInputData(lstPhuong[i]), tenQuanHuyen);
                    }
                }
            }

            if (0 == tenQuanHuyen.Length)
            {
                List<string> lstQuanHuyen = this._slstQuanHuyenDTO.Where(x => x.Key.StartsWith(strInput) | strInput.StartsWith(x.Key)).Select(x => x.Value).ToList<string>();
                string strTemp = String.Empty;
                for (int i = 0; i < lstQuanHuyen.Count; i++)
                {
                    string keyword = Utilities.FormatInputData(lstQuanHuyen[i]);
                    if (keyword.StartsWith(strInput))
                    {
                        strTemp = String.Empty;
                    }
                    else
                    {
                        strTemp = strInput.Replace(keyword, String.Empty).Trim();
                    }

                    Analysis(strTemp, tenDichVu, tenDuong, tenPhuong, lstQuanHuyen[i]);
                    if (lstQuanHuyen[i].ToLower() != Utilities.FormatInputData(lstQuanHuyen[i]))
                    {
                        Analysis(strTemp, tenDichVu, tenDuong, tenPhuong, Utilities.FormatInputData(lstQuanHuyen[i]));
                    }
                }
            }
        }

        public List<string> Suggest(string strInput)
        {
            List<string> lstRet = null;
            try
            {
                if (null != this._lstResult)
                {
                    this._lstResult.Clear();
                }
                else
                {
                    this._lstResult = new List<string>();
                }

                strInput = Utilities.FormatInputData(strInput);
                this.Analysis(strInput, String.Empty, String.Empty, String.Empty, String.Empty);
                lstRet = this._lstResult;
            }
            catch (System.Exception ex)
            {
                lstRet = new List<string>();
            }

            return lstRet;
        }

        public List<TuKhoaDiaDiemDTO> AutoCompleteTuKhoaDiaDiem(String strInput)
        {
            strInput = Utilities.FormatInputData(strInput);
            List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiem = new List<TuKhoaDiaDiemDTO>();
            try
            {
                DataProvider.Connect();
                lstTuKhoaDiaDiem = this._tuKhoaDiaDiemDAO.AutoCompleteTuKhoaDiaDiem(strInput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstTuKhoaDiaDiem;
        }

        public List<TuKhoaDuongDTO> AutoCompleteTuKhoaDuong(String strInput)
        {
            strInput = Utilities.FormatInputData(strInput);
            List<TuKhoaDuongDTO> lstTuKhoaDuong = new List<TuKhoaDuongDTO>();
            try
            {
                DataProvider.Connect();
                lstTuKhoaDuong = this._tuKhoaDuongDAO.AutoCompleteTuKhoaDuong(strInput);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstTuKhoaDuong;
        }

        public List<TuKhoaDiaDiemDTO> AutoCompleteTuKhoaDiaDiemTheoDichVu(String strInput, int MaDichVu)
        {
            strInput = Utilities.FormatInputData(strInput);
            List<TuKhoaDiaDiemDTO> lstTuKhoaDiaDiem = new List<TuKhoaDiaDiemDTO>();
            try
            {
                DataProvider.Connect();
                lstTuKhoaDiaDiem = this._tuKhoaDiaDiemDAO.AutoCompleteTuKhoaDiaDiemTheoDichVu(strInput, MaDichVu);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstTuKhoaDiaDiem;
        }

        public List<TuKhoaDuongDTO> AutoCompleteTuKhoaDuongTheoDichVu(String strInput, int MaDichVu)
        {
            strInput = Utilities.FormatInputData(strInput);
            strInput.Trim();
            List<TuKhoaDuongDTO> lstTuKhoaDuong = new List<TuKhoaDuongDTO>();
            try
            {
                DataProvider.Connect();
                lstTuKhoaDuong = this._tuKhoaDuongDAO.AutoCompleteTuKhoaDuongTheoDichVu(strInput, MaDichVu);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DataProvider.Disconnect();
            }

            return lstTuKhoaDuong;
        }
    }
}
